import * as appConfig from 'app-config.js'

App({
  onLaunch(){
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
 
    updateManager.onUpdateReady(function () {
      wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
              if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
              }
          }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
          title: '更新提示',
          content: '新版本下载失败',
          showCancel:false
      })
    })
  },
	errorMsg(title){
		wx.showToast({
			title,
			icon:'none'
		})
	},
	getMemInfo(){
    var promise = new Promise((resolve,reject)=>{
      wx.getStorage({
        key:'memInfo',
        success(res){
          resolve(res)
        },
        fail(error){
          app.errorMsg(error.errMsg)
        }
      })
    })   
    return promise 
  },
  request : function(url, data, method){
      var promise = new Promise((resolve, reject) => {
         //init
         var that = this;
         var postData = data;
         /*
         //自动添加签名字段到postData，makeSign(obj)是一个自定义的生成签名字符串的函数
         postData.signature = that.makeSign(postData);
         */
         //网络请求
         wx.request({
            url:appConfig.apiBase+url,
            data: postData,
            method,
            success: function (res) {
              resolve(res.data)
               if (res.data.code == 0) {
                  resolve( res.data );
               } else {
                  reject( res.data );
               }
            },
            error: function (e) {
               reject('网络出错');
            }
         })
      });
      return promise;
   },


 })

 

function memInfo(url) {
  try {
    var value = wx.getStorageSync('memInfo')
    if (value) {
      return value;
    }else{
      wx.navigateTo({
        url: url,
      })
    }
   
  } catch (e) {
    wx.navigateTo({
      url: url,
    })
  }
}


module.exports = {
  memInfo: memInfo
}
